package com.self.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.self.admin.mapper.SysRolePrivilegeMapper;
import com.self.admin.model.SysMenu;
import com.self.admin.model.SysPrivilege;
import com.self.admin.model.SysRolePrivilege;
import com.self.admin.service.SysMenuService;
import com.self.admin.service.SysPrivilegeService;
import com.self.admin.service.SysRolePrivilegeService;
import com.self.admin.vo.RolePrivilegesParamVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SysRolePrivilegeServiceImpl extends ServiceImpl<SysRolePrivilegeMapper, SysRolePrivilege> implements SysRolePrivilegeService{

    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private SysPrivilegeService sysPrivilegeService;
    @Override
    public List<SysMenu> findSysMenuAndPrivileges(Long roleId) {
        List<SysMenu> list = sysMenuService.list();
        if (!CollectionUtils.isEmpty(list)){
            List<SysMenu> oneLevelMenus = list.stream().filter(x -> x.getParentId() == null).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(oneLevelMenus)){
                List<SysMenu> sysMenuList = Lists.newArrayList();
                for (SysMenu oneLevel : oneLevelMenus){
                    sysMenuList.addAll(getChildren(oneLevel.getId(),roleId,list));
                }
                return sysMenuList;
            }

        }
        return Collections.emptyList();
    }

    private List<SysMenu> getChildren(Long parentId, Long roleId, List<SysMenu> list) {
        List<SysMenu> child = Lists.newArrayList();
        for (SysMenu menu : list){
            if (menu.getParentId() != null && parentId.equals(menu.getParentId())){
                List<SysPrivilege> sysPrivileges = sysPrivilegeService.list(new LambdaQueryWrapper<SysPrivilege>().eq(SysPrivilege::getMenuId, menu.getId()));
                if (!CollectionUtils.isEmpty(sysPrivileges)){
                    sysPrivileges.forEach(sysPrivilege -> {
                        int count = count(new LambdaQueryWrapper<SysRolePrivilege>()
                                .eq(SysRolePrivilege::getRoleId, roleId)
                                .eq(SysRolePrivilege::getPrivilegeId, sysPrivilege.getId()));
                        if (count > 0){
                            sysPrivilege.setOwn(1);
                        }else{
                            sysPrivilege.setOwn(0);
                        }
                    });
                }
                menu.setSysPrivileges(sysPrivileges);
                menu.setChilds(getChildren(menu.getId(),roleId,list));
                child.add(menu);
            }
        }
        return child;
    }

    @Transactional
    @Override
    public void grantPrivileges(RolePrivilegesParamVo rolePrivilegesParam) {
        //1 先删除之前该角色的权限
        this.remove(new LambdaQueryWrapper<SysRolePrivilege>()
                .eq(SysRolePrivilege::getRoleId, rolePrivilegesParam.getRoleId()));
        List<SysRolePrivilege> collect = rolePrivilegesParam.getPrivilegeIds().stream().map(privilegeId -> {
            SysRolePrivilege sysRolePrivilege = new SysRolePrivilege();
            sysRolePrivilege.setPrivilegeId(privilegeId);
            sysRolePrivilege.setRoleId(rolePrivilegesParam.getRoleId());
            return sysRolePrivilege;
        }).collect(Collectors.toList());
        this.saveBatch(collect);
    }
}
